package com.company.linkedlist;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        SingleLinkedList sll=new SingleLinkedList();
        HeroNode heroNode0 = new HeroNode(1001,"盖伦","德玛西亚之力");
        HeroNode heroNode1 = new HeroNode(1004,"拉克丝","光辉女神");
        HeroNode heroNode2 = new HeroNode(1007,"伊泽瑞尔","探险家");
        HeroNode heroNode3 = new HeroNode(1002,"伊泽瑞尔","探险家");
        HeroNode heroNode4 = new HeroNode(1005,"伊泽尔","冒险家");

        sll.add(heroNode0);
        sll.add(heroNode2);
        sll.addByOrder(heroNode1);
        sll.addByOrder(heroNode3);
        sll.update(heroNode4);
        sll.showAll();
        System.out.println(getLength(sll.getHead()));
        System.out.println(reciprocal(sll.getHead(),3));
        System.out.println("-------------------");
        HeroNode resll=reverse(sll.getHead());
        while (resll!=sll.getHead()){
            System.out.println(resll);
            resll=resll.next;
        }
    }
    public static int getLength(HeroNode heroNode){
        HeroNode temp = heroNode.next;
        int num=0;
        while (temp!=null){
            num++;
            temp=temp.next;
        }
        return num;
    }

    public static HeroNode reciprocal(HeroNode head,int n){
        if (head.next==null){
            return null;
        }
        HeroNode pre=head;
        HeroNode cur=head;
        int num=0;
        while(cur.next!=null){
            cur=cur.next;
            num++;
            if (num>=n){
                pre=pre.next;
            }
        }
        return pre;
    }

    public static HeroNode reverse(HeroNode head){
        if (head.next==null||head.next.next==null){
            return head;
        }
        HeroNode pre=head;
        HeroNode cur=head.next;
        pre.next=null;
        while (cur!=null){
            HeroNode temp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=temp;
        }
        return pre;
    }

}

class SingleLinkedList{
    private HeroNode head=new HeroNode(0,"","");

    public void add(HeroNode heroNode){
        HeroNode temp=head;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=heroNode;
        heroNode.next=null;
    }

    public void addByOrder(HeroNode heroNode){
        HeroNode temp = head;
        int heroNo=heroNode.no;
        while (temp.next!=null){
            if (temp.next.no>heroNo){
                heroNode.next=temp.next;
                temp.next=heroNode;
                break;
            }else if(temp.next.no==heroNo){
                System.out.println("准备插入的英雄编号"+heroNo+"已经存在，不能加入");
                break;
            }
            temp=temp.next;
        }
        if (temp.next==null){
            temp.next=heroNode;
            heroNode.next=null;
        }
    }

    public void update(HeroNode newHeroNode){
        int heroNo=newHeroNode.no;
        HeroNode temp=head.next;
        if (temp==null){
            System.out.println("链表为空~");
            return;
        }
        while (temp!=null){
            if (temp.no==heroNo){
                temp.name=newHeroNode.name;
                temp.nickname=newHeroNode.nickname;
                System.out.println("修改成功！");
                break;
            }
            temp=temp.next;
        }
        if (temp==null){
            System.out.println("没有找到该编号");
        }
    }

    public void delete(int heroNo){
        HeroNode temp = head;
        if (temp.next==null){
            System.out.println("该链表为空~");
            return;
        }
        while (temp.next!=null){
            if (temp.next.no==heroNo){
                temp.next=temp.next.next;
                System.out.println("删除成功");
                break;
            }
            temp=temp.next;
        }
        if (temp.next==null){
            System.out.println("没有找到该英雄~");
        }
    }

    public void showAll(){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=head.next;
        while (temp!=null){
            System.out.println("英雄编号："+temp.no+"\t英雄姓名："+temp.name+"\t英雄昵称："+temp.nickname);
            temp=temp.next;
        }
    }

    public HeroNode getHead() {
        return head;
    }
}



class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode() {
    }

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname +
                '}';
    }
}